import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

// 自定义线程池
class MyThreadPool{
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    // 构造方法，创建线程，且取出任务队列中的任务，在进行执行。
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                while (true){
                    try {
                        Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }
    // 向任务队列添加任务
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

}
public class Main {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool = new MyThreadPool(10);
        for (int j = 0; j < 100; j++) {
            int n = j;
            pool.submit(new Runnable() {  // runnable描述每个任务
                @Override
                public void run() {
                    System.out.println(n);
                }
            });
        }
    }
}
